package com.youmingxing.activity.selfInfo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.utils.StringUtil;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.TextCountWatcher;

import org.json.JSONObject;


/**
 * Created by JHS on 2015/9/10
 */
public class FeedbackActivity extends BaseActivity {


    private EditText feedbackEditText;
    private EditText feedbackContactEdt;
    private Button confirmButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_feedback);
        initView();
    }

    private void initView() {
        setTopbarName(R.string.settingtitle_feedback);
        feedbackEditText = (EditText) findViewById(R.id.feedback_content_edt);
        feedbackContactEdt = (EditText) findViewById(R.id.feedback_contact_edt);

        confirmButton = (Button) findViewById(R.id.confirm_feedback);

        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String contentStr = feedbackEditText.getText().toString();
                String contactStr = feedbackContactEdt.getText().toString();

                if (StringUtil.isEmpty(contentStr)) {
                    showToast(R.string.feeback_content_empty);
                } else {
                    SelfInfoApi.feedback(contentStr,contactStr,new BaseJsonHandler(FeedbackActivity.this){
                        @Override
                        public void onSuccessWithData(JSONObject response) {
                            if (response.optJSONObject("data").optInt("result") == 1){
                                showToast("反馈成功");
                                finish();
                            } else {
                                showToast("出错啦");
                            }
                        }
                    });
                }
            }
        });
        notClick();
        feedbackEditText.addTextChangedListener(new TextCountWatcher(1) {
            @Override
            public void onTextMatchCount() {
                canClick();
            }

            @Override
            public void onTextLessThanCount() {
                notClick();
            }
        });
    }

    private void canClick() {
        confirmButton.setBackgroundResource(R.drawable.btn_redbg);
        confirmButton.setClickable(true);
    }

    private void notClick() {
        confirmButton.setBackgroundResource(R.drawable.btn_unable);
        confirmButton.setClickable(false);
    }

}